package com.hypo.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hypo.business.domain.dto.WarehouseOpDto;
import com.hypo.business.domain.dto.PageResultDto;
import com.hypo.business.domain.dto.WarehousePageDto;
import com.hypo.business.domain.po.Pcd;
import com.hypo.business.domain.po.Warehouse;
import com.hypo.business.domain.po.WarehouseArea;
import com.hypo.business.mapper.PcdMapper;
import com.hypo.business.mapper.WarehouseAreaMapper;
import com.hypo.business.mapper.WarehouseMapper;
import com.hypo.business.service.WarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hypo
 * @version 1.0
 * @since 2025/8/13
 */
@Service
@RequiredArgsConstructor
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    private final WarehouseMapper warehouseMapper;

    private final PcdMapper pcdMapper;

    private final WarehouseAreaMapper warehouseAreaMapper;

    /**
     * 删除
     */
    @Override
    public int warehouseDelete(Integer id) {
        List<WarehouseArea> warehouseAreas = warehouseAreaMapper.selectList(Wrappers.<WarehouseArea>lambdaQuery()
                .eq(WarehouseArea::getWarehouseId, id));
        if (ObjectUtil.isEmpty(warehouseAreas)) {
            return warehouseMapper.deleteById(id);
        }
        return 0;
    }

    /**
     * 查询全部仓库
     */
    @Override
    public List<Warehouse> queryWarehouse() {
        return warehouseMapper.selectList(Wrappers.<Warehouse>lambdaQuery()
                .eq(Warehouse::getIsDel, 1));
    }

    /**
     * 修改
     */
    @Override
    public void warehouseUpdate(WarehouseOpDto dto) {
        Warehouse warehouse = new Warehouse();
        BeanUtil.copyProperties(dto, warehouse);
        extracted(dto, warehouse);
        warehouseMapper.updateById(warehouse);
    }

    /**
     * 省份地址地区处理
     */
    private void extracted(WarehouseOpDto dto, Warehouse warehouse) {
        Pcd pcd = pcdMapper.selectOne(Wrappers.<Pcd>lambdaQuery()
                .eq(Pcd::getId, dto.getCityId()));
        //设置城市名字及省份id
        warehouse.setCityName(pcd.getName());
        warehouse.setProvinceId(pcd.getParentId());
        //设置省份名字
        Pcd pcd1 = pcdMapper.selectOne(Wrappers.<Pcd>lambdaQuery()
                .eq(Pcd::getId, pcd.getParentId()));
        warehouse.setProvinceName(pcd1.getName());
    }

    /**
     * 新增仓库
     */
    @Override
    public void warehouseAdd(WarehouseOpDto dto) {
        Warehouse warehouse = new Warehouse();
        BeanUtil.copyProperties(dto, warehouse);
        warehouse.setCreateTime(LocalDateTime.now());
        warehouse.setUpdateTime(LocalDateTime.now());
        extracted(dto, warehouse);
        //设置负责人id使用糊涂工具雪花算法
        warehouse.setPersonChargeId(IdUtil.getSnowflakeNextId());
        warehouseMapper.insert(warehouse);
    }

    /**
     * 分页查询仓库
     */
    @Override
    public PageResultDto<Warehouse> warehousePage(WarehousePageDto dto) {
        Page<Warehouse> page = new Page<>(dto.getPage(), dto.getPageSize());
        Page<Warehouse> warehousePage = warehouseMapper.selectPage(page, Wrappers.<Warehouse>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(dto.getName()), Warehouse::getName, dto.getName())
                .eq(ObjectUtil.isNotEmpty(dto.getCityId()), Warehouse::getCityId, dto.getCityId())
                .like(ObjectUtil.isNotEmpty(dto.getPersonChargeName()), Warehouse::getPersonChargeName, dto.getPersonChargeName()));
        return new PageResultDto<>(warehousePage.getTotal(), warehousePage.getRecords());
    }
}
